/**
 *
 */
package com.zhuoyue.zhtj.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.eit.eitsite.common.config.Global;
import com.eit.eitsite.common.persistence.Page;
import com.eit.eitsite.common.web.BaseController;
import com.eit.eitsite.common.utils.StringUtils;
import com.zhuoyue.zhtj.entity.Tclass;
import com.zhuoyue.zhtj.service.TclassService;

/**
 * 所带班级Controller
 * @author hss
 * @version 2017-09-28
 */
@Controller
@RequestMapping(value = "${adminPath}/zhtj/tclass")
public class TclassController extends BaseController {

	@Autowired
	private TclassService tclassService;
	
	@ModelAttribute
	public Tclass get(@RequestParam(required=false) String id) {
		Tclass entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = tclassService.get(id);
		}
		if (entity == null){
			entity = new Tclass();
		}
		return entity;
	}
	
	@RequiresPermissions("zhtj:tclass:view")
	@RequestMapping(value = {"list", ""})
	public String list(Tclass tclass, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Tclass> page = tclassService.findPage(new Page<Tclass>(request, response), tclass); 
		model.addAttribute("page", page);
		return "zhuoyue/zhtj/tclassList";
	}

	@RequiresPermissions("zhtj:tclass:view")
	@RequestMapping(value = "form")
	public String form(Tclass tclass, Model model) {
		model.addAttribute("tclass", tclass);
		return "zhuoyue/zhtj/tclassForm";
	}

	@RequiresPermissions("zhtj:tclass:edit")
	@RequestMapping(value = "save")
	public String save(Tclass tclass, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, tclass)){
			return form(tclass, model);
		}
		tclassService.save(tclass);
		addMessage(redirectAttributes, "保存所带班级成功");
		return "redirect:"+Global.getAdminPath()+"/zhtj/tclass/?repage";
	}
	
	@RequiresPermissions("zhtj:tclass:edit")
	@RequestMapping(value = "delete")
	public String delete(Tclass tclass, RedirectAttributes redirectAttributes) {
		tclassService.delete(tclass);
		addMessage(redirectAttributes, "删除所带班级成功");
		return "redirect:"+Global.getAdminPath()+"/zhtj/tclass/?repage";
	}

}